package xiaohong.data.array;

import java.util.HashMap;

public class RecursionDemo {


    public static void main(String[] args) {
        int[][] map = new int[10][10];

        for (int i = 0; i < 10; i++) {
            map[0][i] = 1;
            map[9][i] = 1;
        }

        for (int i = 0; i < 10; i++) {
            map[i][0] = 1;
            map[i][9] = 1;
        }

        map[3][1] = 1;
        map[3][2] = 1;
        map[3][4] = 1;
        map[3][5] = 1;
        map[3][6] = 1;
        map[3][7] = 1;
        map[3][8] = 1;
        map[8][4] = 1;

        System.out.println("地图的路径为");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(map[i][j] + "  ");
            }
            System.out.println();
        }

        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("setWay1", 0);
        setWay3(map , 1 , 1 , hashMap);

        System.out.println("小人走过的路径为");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(map[i][j] + "  ");
            }
            System.out.println();
        }
        System.out.println(hashMap.get("setWay1"));
    }



    public static boolean setWay1(int[][] map , int i , int j, HashMap<String, Integer> hashMap) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay1(map, i + 1, j, hashMap)) {    // 向下走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay1(map, i, j + 1, hashMap)) {  // 向右走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay1(map, i - 1, j, hashMap)) {  // 向上走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay1(map, i, j - 1, hashMap)) {  // 向左走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }


    public static boolean setWay2(int[][] map , int i , int j, HashMap<String, Integer> hashMap) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay2(map, i + 1, j, hashMap)) {    // 向下走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay2(map, i, j + 1, hashMap)) {  // 向右走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay2(map, i, j-1, hashMap)) {  // 向左走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay2(map, i-1, j, hashMap)) {  // 向上走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }


    public static boolean setWay3(int[][] map , int i , int j, HashMap<String, Integer> hashMap) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay3(map, i + 1, j, hashMap)) {    // 向下走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay3(map, i - 1, j, hashMap)) {  // 向上走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay3(map, i, j + 1, hashMap)) {  // 向右走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else if (setWay3(map, i, j - 1, hashMap)) {  // 向左走
                    hashMap.compute("setWay1", (k, integer) -> integer + 1);
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }

    public static boolean setWay4(int[][] map , int i , int j) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay4(map, i + 1, j)) {    // 向下走
                    return false ;
                }else if (setWay4(map, i - 1, j)) {  // 向上走
                    return false ;
                }else if (setWay4(map, i, j - 1)) {  // 向左走
                    return false ;
                }else if (setWay4(map, i, j + 1)) {  // 向右走
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }


    public static boolean setWay5(int[][] map , int i , int j) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay5(map, i + 1, j)) {    // 向下走
                    return false ;
                }else if (setWay5(map, i, j - 1)) {  // 向右走
                    return false ;
                }else if (setWay5(map, i , j + 1)) {
                    return false ;
                }else if (setWay5(map, i + 1, j)) {
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }



    public static boolean setWay6(int[][] map , int i , int j) {
        if (map[8][8] == 2) {
            return true;
        }else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (setWay6(map, i + 1, j)) {    // 向下走
                    return false ;
                }else if (setWay6(map, i, j + 1)) {  // 向右走
                    return false ;
                }else if (setWay6(map, i - 1, j)) {  // 向上走
                    return false ;
                }else if (setWay6(map, i, j - 1)) {  // 向左走
                    return false ;
                }else {
                    map[i][j] = 3;
                    return false ;
                }
            } else {
                return false;
            }
        }
    }





}
